<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2019/1/12
 * Time: 0:24
 */

namespace api\storage\service\inventory;

use api\storage\model\ContainersModel;
use api\storage\model\ReelsModel;
use app\storage\model\ContainerModel;

/**
 * 容器盘点
 *
 * Class ContainerInventoryService
 * @package api\storage\service\inventory
 */
class ContainerInventoryService extends BaseInventoryService
{

    /**
     * 容器实现类的参数检查
     *
     * @param $code
     * @param $storageUuid
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function afterCheck($code, $storageUuid)
    {
        $containersModel = new ContainersModel();
        $container = $containersModel->findByContainerCode($code);
        if (empty($container) || empty($container->getData())) {
            return $this->resultTemplate(-21);
        } else {
            return $this->resultTemplate(1, ['value' => $code]);
        }
    }

    protected function getReels($code, $storageUuid)
    {
        $reelsModel = new ReelsModel();
        $reels = $reelsModel->queryByContainer($code, $storageUuid)->toArray();
        //设置占用
        $this->remakeFlag($code, $storageUuid, true);
        return [
            'reels' => $reels,
            'seatId' => []
        ];
    }

    protected function handleReels($reels)
    {
        $temp = [];
        foreach ($reels['reels'] as $item) {
            $item['isFinish'] = false;
            $item['seatId'] = '';
            $item['outCount'] = $item['count'];
            $temp[] = $item;
        }
        unset($reels['reels']);
        $reels['reels'] = $temp;
        return $reels;
    }


    protected function remakeFlag($code, $storageUuid, $isOccupy)
    {
        $containersModel = new ContainersModel();
        $flag = $isOccupy ? 1 : 0;
        return $containersModel->updateFlag($code, $flag);
    }

}